/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package opk.Generator;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import opk.Solver.ResultShape;

/**
 *
 * @author mikeqcp
 */
public class MyLogger {

    File file;
    BufferedWriter writer = null;
    String data;

    Date start;

    public MyLogger(String data)
    {
        this.start = new Date();
        this.data = data;
    }


    private void coma() throws IOException
    {
        writer.write("\t");
    }

    public void logIn(int algNumber, ResultShape resultShape, ResultShape stopped)
    {
        if (algNumber == 0) file = new File("D://logD.txt");
        if (algNumber == 1) file = new File("D://logH.txt");
        if (algNumber == 2) file = new File("D://logDH.txt");
        try 
        {
            writer = new BufferedWriter(new FileWriter(file, true));

            writer.write(Long.toString(getLongitude(start, new Date())));
            coma();
            writer.write(data);
            coma();

            if(resultShape!=null)
            {
                if (resultShape.getEdgesList() == null || resultShape.getEdgesList().get(0) == null) writer.write("brak wyniku"); else
                writer.write(resultShape.getNumberOfShapes() + "\t" + resultShape.getPerimeter() + "\t" + resultShape.getRatio());
            }
            else
            {
                writer.write("null\t");
            }
            
            writer.write("\t(\t");
                if(stopped!=null)
                { if (stopped.getEdgesList() == null || stopped.getEdgesList().get(0) == null) writer.write("brak wyniku"); else
                    writer.write(stopped.getNumberOfShapes() + "\t" + stopped.getPerimeter() + "\t" + stopped.getRatio());
                }
                else
                {
                    writer.write("null");
                }
            writer.write("\t)");

            writer.newLine();
            writer.flush();
            writer.close();

        } catch (IOException ex){}
        finally
        {
            try {
                writer.close();
            } catch (Exception ex) {}
        }


        
    }

    private String getTime()
    {
        Date time = new Date();
        return time.toString();
    }

    private long getLongitude(Date a, Date b)
    {
        return  Math.abs(a.getTime() - b.getTime());
    }
    
}
